from sys import argv
import asyncio

#
import pydash
from humanfriendly import parse_size

#
from common import *
from metadbconn import *
from videoobj import *

#
import mode_common
import mode_ghs


async def main(mode: str):
    redirect_logger("MetaDB")
    # ================metadbConn区================
    config_cacheDB = CONFIG["cacheDB"]
    with CachedbConn("cache", parse_size(config_cacheDB["map_size"]), config_cacheDB["expiration"]) as cacheDB_conn:
        async with (
            TmdbConn(cacheDB_conn=cacheDB_conn, **pydash.get(CONFIG, "metaDB.tmdb")) as tmdb_conn,
            BgmtvConn(cacheDB_conn=cacheDB_conn, **pydash.get(CONFIG, "metaDB.bgmtv")) as bgmtv_conn,
        ):
            metadb_conns = {"tmdb": tmdb_conn, "bgmtv": bgmtv_conn}
            for cc in metadb_conns.keys():
                cacheDB_conn.vaccum_by_expiration(cc)
            # ================正式工作区================
            if mode == "common":
                redirect_logger("VideoObj")
                await mode_common.work(metadb_conns)
            if mode == "ghs":
                redirect_logger("ModeGHS")
                await mode_ghs.work(metadb_conns)


if __name__ == "__main__":
    if len(argv) > 1:
        mode = argv[1]
    else:
        mode = "common"
    asyncio.run(main(mode))
